package com.common.platform.sys.log.factory;

import com.common.platform.base.config.context.SpringContextHolder;
import com.common.platform.base.utils.CoreUtil;
import com.common.platform.sys.log.LogManager;
import com.common.platform.sys.modular.system.entity.LoginLog;
import com.common.platform.sys.modular.system.entity.OperationLog;
import com.common.platform.sys.modular.system.mapper.LoginLogMapper;
import com.common.platform.sys.modular.system.mapper.OperationLogMapper;
import com.common.platform.sys.state.LogSucceed;
import com.common.platform.sys.state.LogType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.TimerTask;

public class LogTaskFactory {

    private static Logger logger = LoggerFactory.getLogger(LogManager.class);
    private static LoginLogMapper loginLogMapper = SpringContextHolder.getBean(LoginLogMapper.class);
    private static OperationLogMapper operationLogMapper = SpringContextHolder.getBean(OperationLogMapper.class);

    public static TimerTask loginLog(final Long userId, final String ip) {
        return new TimerTask() {
            @Override
            public void run() {
                try {
                    LoginLog loginLog = LogFactory.createLoginLog(LogType.LOGIN, userId, null, ip);
                    loginLogMapper.insert(loginLog);
                } catch (Exception e) {
                    logger.error("创建登录日志异常！", e);
                }
            }
        };
    }

    public static TimerTask loginLog(final String username, final String msg, final String ip) {
        return new TimerTask() {
            @Override
            public void run() {
                try {
                    LoginLog loginLog = LogFactory.createLoginLog(LogType.LOGIN_FAIL, null, "账号：" + username + "，" + msg, ip);
                    loginLogMapper.insert(loginLog);
                } catch (Exception e) {
                    logger.error("创建登录失败日志异常！", e);
                }
            }
        };
    }

    public static TimerTask exitLog(final Long userId, final String ip) {
        return new TimerTask() {
            @Override
            public void run() {
                try {
                    LoginLog loginLog = LogFactory.createLoginLog(LogType.EXIT, userId, null, ip);
                    loginLogMapper.insert(loginLog);
                } catch (Exception e) {
                    logger.error("创建退出登录日志异常！", e);
                }
            }
        };
    }

    public static TimerTask bussinessLog(final Long userId, final String bussinessName,
                                         final String clazzName, final String methodName, final String msg) {
        return new TimerTask() {
            @Override
            public void run() {
                try {
                    OperationLog operationLog = LogFactory.createOperationLog(LogType.BUSSINESS, userId, bussinessName,
                            clazzName, methodName, msg, LogSucceed.SUCCESS);
                    operationLogMapper.insert(operationLog);
                } catch (Exception e) {
                    logger.error("创建业务日志异常！", e);
                }
            }
        };
    }

    public static TimerTask exceptionLog(final Long userId, final Exception exception) {
        return new TimerTask() {
            @Override
            public void run() {
                try {
                    String msg = CoreUtil.getExceptionMsg(exception);
                    OperationLog operationLog = LogFactory.createOperationLog(LogType.EXCEPTION, userId, "",
                            null, null, msg, LogSucceed.FAIL);
                    operationLogMapper.insert(operationLog);
                } catch (Exception e) {
                    logger.error("创建异常日志异常！", e);
                }
            }
        };
    }
}
